/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.jboss.netty.channel;

import java.net.SocketAddress;

import org.jboss.netty.buffer.ChannelBuffer;

/**
 * A {@link ChannelHandler} which provides an individual handler method for each
 * event type. This handler down-casts the received upstream or or downstream
 * event into more meaningful sub-type event and calls an appropriate handler
 * method with the down-cast event. For an upstream event, the names of the
 * methods are identical to the upstream event names, as introduced in the
 * {@link ChannelEvent} documentation. For a downstream event, the names of the
 * methods starts with the name of the operation and ends with
 * {@code "Requested"} (e.g.
 * {@link #writeRequested(ChannelHandlerContext, MessageEvent) writeRequested}.)
 * <p>
 * Please use {@link SimpleChannelUpstreamHandler} or
 * {@link SimpleChannelDownstreamHandler} if you want to intercept only upstream
 * or downstream events.
 * 
 * <h3>Overriding the
 * {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream}
 * and {@link #handleDownstream(ChannelHandlerContext, ChannelEvent)
 * handleDownstream} method</h3>
 * <p>
 * You can override the
 * {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream}
 * and {@link #handleDownstream(ChannelHandlerContext, ChannelEvent)
 * handleDownstream} method just like overriding an ordinary Java method. Please
 * make sure to call {@code super.handleUpstream()} or
 * {@code super.handleDownstream()} so that other handler methods are invoked
 * properly:
 * </p>
 * 
 * <pre>
 * public class MyChannelHandler extends {@link SimpleChannelHandler} {
 * 
 *     {@code @Override}
 *     public void handleUpstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
 * 
 *         // Log all channel state changes.
 *         if (e instanceof {@link ChannelStateEvent}) {
 *             logger.info("Channel state changed: " + e);
 *         }
 * 
 *         <strong>super.handleUpstream(ctx, e);</strong>
 *     }
 * 
 *     {@code @Override}
 *     public void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
 * 
 *         // Log all channel state changes.
 *         if (e instanceof {@link MessageEvent}) {
 *             logger.info("Writing:: " + e);
 *         }
 * 
 *         <strong>super.handleDownstream(ctx, e);</strong>
 *     }
 * }
 * </pre>
 */
public class SimpleChannelHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler {

	/**
	 * {@inheritDoc} Down-casts the received upstream event into more meaningful
	 * sub-type event and calls an appropriate handler method with the
	 * down-casted event.
	 */
	public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {

		if (e instanceof MessageEvent) {
			messageReceived(ctx, (MessageEvent) e);
		} else if (e instanceof WriteCompletionEvent) {
			WriteCompletionEvent evt = (WriteCompletionEvent) e;
			writeComplete(ctx, evt);
		} else if (e instanceof ChildChannelStateEvent) {
			ChildChannelStateEvent evt = (ChildChannelStateEvent) e;
			if (evt.getChildChannel().isOpen()) {
				childChannelOpen(ctx, evt);
			} else {
				childChannelClosed(ctx, evt);
			}
		} else if (e instanceof ChannelStateEvent) {
			ChannelStateEvent evt = (ChannelStateEvent) e;
			switch (evt.getState()) {
			case OPEN:
				if (Boolean.TRUE.equals(evt.getValue())) {
					channelOpen(ctx, evt);
				} else {
					channelClosed(ctx, evt);
				}
				break;
			case BOUND:
				if (evt.getValue() != null) {
					channelBound(ctx, evt);
				} else {
					channelUnbound(ctx, evt);
				}
				break;
			case CONNECTED:
				if (evt.getValue() != null) {
					channelConnected(ctx, evt);
				} else {
					channelDisconnected(ctx, evt);
				}
				break;
			case INTEREST_OPS:
				channelInterestChanged(ctx, evt);
				break;
			default:
				ctx.sendUpstream(e);
			}
		} else if (e instanceof ExceptionEvent) {
			exceptionCaught(ctx, (ExceptionEvent) e);
		} else {
			ctx.sendUpstream(e);
		}
	}

	/**
	 * Invoked when a message object (e.g: {@link ChannelBuffer}) was received
	 * from a remote peer.
	 */
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when an exception was raised by an I/O thread or a
	 * {@link ChannelHandler}.
	 */
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
		if (this == ctx.getPipeline().getLast()) {
		}
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} is open, but not bound nor connected.
	 */
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} is open and bound to a local address, but
	 * not connected.
	 */
	public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} is open, bound to a local address, and
	 * connected to a remote address.
	 */
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel}'s {@link Channel#getInterestOps()
	 * interestOps} was changed.
	 */
	public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} was disconnected from its remote peer.
	 */
	public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} was unbound from the current local
	 * address.
	 */
	public void channelUnbound(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a {@link Channel} was closed and all its related resources
	 * were released.
	 */
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when something was written into a {@link Channel}.
	 */
	public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a child {@link Channel} was open. (e.g. a server channel
	 * accepted a connection)
	 */
	public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * Invoked when a child {@link Channel} was closed. (e.g. the accepted
	 * connection was closed)
	 */
	public void childChannelClosed(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
		ctx.sendUpstream(e);
	}

	/**
	 * {@inheritDoc} Down-casts the received downstream event into more
	 * meaningful sub-type event and calls an appropriate handler method with
	 * the down-casted event.
	 */
	public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {

		if (e instanceof MessageEvent) {
			writeRequested(ctx, (MessageEvent) e);
		} else if (e instanceof ChannelStateEvent) {
			ChannelStateEvent evt = (ChannelStateEvent) e;
			switch (evt.getState()) {
			case OPEN:
				if (!Boolean.TRUE.equals(evt.getValue())) {
					closeRequested(ctx, evt);
				}
				break;
			case BOUND:
				if (evt.getValue() != null) {
					bindRequested(ctx, evt);
				} else {
					unbindRequested(ctx, evt);
				}
				break;
			case CONNECTED:
				if (evt.getValue() != null) {
					connectRequested(ctx, evt);
				} else {
					disconnectRequested(ctx, evt);
				}
				break;
			case INTEREST_OPS:
				setInterestOpsRequested(ctx, evt);
				break;
			default:
				ctx.sendDownstream(e);
			}
		} else {
			ctx.sendDownstream(e);
		}
	}

	/**
	 * Invoked when {@link Channel#write(Object)} is called.
	 */
	public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#bind(SocketAddress)} was called.
	 */
	public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#connect(SocketAddress)} was called.
	 */
	public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#setInterestOps(int)} was called.
	 */
	public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#disconnect()} was called.
	 */
	public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#unbind()} was called.
	 */
	public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}

	/**
	 * Invoked when {@link Channel#close()} was called.
	 */
	public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		ctx.sendDownstream(e);
	}
}
